﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;

namespace ARMS.Models
{
    #region speciality
    public enum Speciality
    {
        All = 0x0,
        Electrician = 0x1,
        Mechanic = 0x2,
        Other = 0x3
    }
    #endregion

    #region class Worker
    public class Worker: Object
    {
        [Required]
        [Display(Name = "User ID")]
        public int id {get; set;}
        
        [Required] 
        [Display(Name = "User Name")]
        public int userid { get; set; }

        [Display(Name = "Worker speciality")]
        public Speciality speciality {get; set;}
    }
    #endregion

    #region class WorkerModel
    public class WorkerModel: ARMS.BaseClasses.BaseModel
    {
        public DataTable findWorker(int id, int shopid = 0, string firstname = "", string lastname = "")
        {
            string select = @"SELECT * FROM workersview";
            string where = @"";

            if (id > 0) where += String.Format(" id = {0}", id);

            if (firstname.Length > 0) where += (where.Length > 0) ?
                String.Format(" AND firstname = {0}", firstname) :
                String.Format(" firstname = {0}", firstname);

            if (lastname.Length > 0) where +=
                (where.Length > 0) ?
                String.Format(" AND lastname = {0}", lastname) :
                String.Format(" last = {0}", lastname);

            string formatedSQL = (where.Length > 0) ? select + " WHERE " + where : select;

            DataTable data = new DataTable();

            data = OpenTable(formatedSQL);

            return data;
        }
        public DataTable getWorkers(int shopid, Speciality specility = Speciality.All, bool activeOnly = false)
        {
            string select = @"SELECT * FROM workersview";
            string where = @"";

            if (shopid > 0) where += String.Format(" shopid = {0}", shopid);
           
            if (activeOnly) where += (where.Length > 0) ? " AND active = 1" : " active = 1";

            if (specility != Speciality.All) where += 
                (where.Length > 0) ?
                String.Format(" AND speciality = {0}", specility) :
                String.Format(" speciality = {0}", specility);

            string formatedSQL = (where.Length > 0) ? select + " WHERE " + where : select;
            formatedSQL += " ORDER BY shopid, speciality, firstname, lastname";

            DataTable data = new DataTable();
            try
            {
                data = OpenTable(formatedSQL);
            }
            catch (Exception e)
            {
                throw new Exception(String.Format("Error fetching workers {0}.", formatedSQL), e);
            }
            return data;
        }
    }
    #endregion
}